package org.xnsg.app.util;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.utils.xx;
import com.eova.model.User;

import java.sql.Timestamp;
import java.util.Date;


public class InterceptUtil extends MetaObjectIntercept {
	public static String className = "com.oss.Intercept.InterceptUtil";

	public static String getUserID(AopContext ac) throws Exception {

		String user_id = "";
		User user = ac.ctrl.getSessionAttr("user");
		String uid = user.getInt("id").toString();
		user_id = ac.user.toString();
		// System.out.println(this.className + "> uid = [" + uid + "]");
		// System.out.println(this.className + "> user_id = [" + user_id + "]");
		return uid;

	}

	public static String getXID(AopContext ac) throws Exception {
		String xid = ac.user.get("xid");
		return xid;
	}

	public static String getLoginID(AopContext ac) throws Exception {
		String login_id = ac.user.get("login_id");
		return login_id;
	}

	public static void queryBefore(AopContext ac, String className, String tableName) throws Exception {
		// DIY查询，名称有值，用名称里面的值来查状态列
		String method = "queryBefore";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");

		String user_id = getUserID(ac);
		String name = "";

		int i = 0;

		if ((!user_id.equals("1")) && (!user_id.equals("2"))) {
			System.out.println(prompt +  "不是管理员,需要限制!");
			while (true) {
				name = ac.ctrl.getPara(i);
				System.out.println(prompt +  "name(" + String.valueOf(i) + ")=[" + name + "]");
				if ( (name.indexOf("c_" + tableName) != -1) || (name.indexOf("c_i_" + tableName) != -1) ) {
					ac.condition = " and ( insertowner = '" + user_id + "' or insertowner in "
							+ " ( select t2.id from pt_user t2, pt_xzqh t3 " + " where t2.xid = t3.xid "
							+ " and t3.fid like ( select t2.xid from pt_user t2 where t2.id = '" + user_id
							+ "' )||'%' )) ";

					System.out.println(prompt +  "ac.condition=[" + ac.condition + "]");
					break;
				} else {
					if (xx.isEmpty(name)) {
						break;
					}
				}
				i++;
			}

		} else {
			System.out.println(prompt + "user_id=[" + user_id + "]");
			System.out.println(prompt + "是管理员,不需要限制!");
		}

		/*
		 * if (!xx.isEmpty(name)) { ac.condition = " state = " + name; }
		 */

		// 同理：想添加自定义查询条件，直接添加元字段
		// 然后配置为 可查询，禁止编辑，禁止新增，然后获取值
		// 利用本AOP方法可自定义查询sql即可
		// 应用场景：like or 子查询 范围查询 外表关联查询...
		System.out.println(prompt + "> end.");
	}

	public String addBefore(AopContext ac, String className) throws Exception {
		String method = "addBefore";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");

		String user_id = InterceptUtil.getUserID(ac);
		ac.record.set("insertowner", user_id);
		ac.record.set("inserttime", new Timestamp(new Date().getTime()));
		System.out.println(prompt + "end.");

		return super.addBefore(ac);
	}

	public static String checkStringLength(AopContext ac, String fieldName, String chineseName, int maxLength)
			throws Exception {
		String vfield = ac.record.get(fieldName).toString();
		if (vfield.length() > maxLength) {
			// System.out.println(className + "." + method + "> vfield 长度超出范围!"
			// );
			// ac.record.set("iapprovalfile", vfield.substring(0,100));
			return chineseName + "的长度超出范围(!" + vfield.length() + "/" + maxLength + ")";
		}
		return "0";
	}
}
